home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / quip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  1.6 KB  |  101 lines

  1. #include    <dg_stdio.h>
  2.  
  3. FILE        *seekfd,        /* the address file */
  4.         *fortfp;        /* the actual fortune file */
  5.  
  6. #include    <quip.h>
  7.  
  8. unsigned    bmap[2048];
  9.  
  10. bittst(bitno)
  11. int    bitno;
  12. {
  13.     return (bmap[bitno / 16] & (1 << (bitno % 16)));
  14. }
  15.  
  16. bitset(bitno)
  17. int    bitno;
  18. {
  19.     bmap[bitno / 16] |= (1 << (bitno % 16));
  20. }
  21.  
  22. openfiles()
  23. {
  24.     seekfd = fopen(seekname, "r");
  25.     if (seekfd == 0) {
  26.         puts("Cannot open address file.");
  27.         exit(0);
  28.     }
  29.     fortfp = fopen(quipname, "r");
  30.     if (fortfp == 0) {
  31.         puts("Cannot open fortunes file.");
  32.         exit(0);
  33.     }
  34. }
  35.  
  36. long
  37. pickfort()
  38. {
  39.     unsigned    size,
  40.             count = 0;
  41.     int        whseek;
  42.     long        whfort;
  43.  
  44.     if (fseek(seekfd,0l,2))
  45.         printf("fseek screwed up\n");
  46.     size = ftell(seekfd) / sizeof(long);
  47.     do {
  48.         whseek = (rand() % (size - 1)) + 1;
  49.         if (count++ > 0177776)
  50.             exit(-1);
  51.     } while (bittst(whseek));
  52.     bitset(whseek);
  53.     fseek(seekfd, (long)whseek * (long)sizeof(long), 0);
  54.     if (fread((char *)&whfort, sizeof(long), 1, seekfd) < 1)
  55.         puts("Read error in address file.");
  56.     return ((long)whfort);
  57. }
  58.  
  59. showfort(addr)
  60. long    addr;
  61. {
  62.     int    lastc = ' ',
  63.         c;
  64.  
  65.     fseek(fortfp, addr, 0);
  66.     for (;;) {
  67.         c = getc(fortfp);
  68.         if (c == '.' && lastc == '\n') {
  69.             return;
  70.         }
  71.         if (c < ' ' && c != '\n' && c != '\007' && c != '\t')
  72.             c = '@';
  73.         putchar(c);
  74.         lastc = c;
  75.     }
  76. }
  77.  
  78. closefiles()
  79. {
  80.     fclose(seekfd);
  81.     fclose(fortfp);
  82. }
  83.  
  84. main(argc, argv)
  85. int    argc;
  86. char    *argv[];
  87. {
  88.     int        times = 1;
  89.  
  90.     if (argc > 1)
  91.         times = atoi(argv[1]);
  92.     srand(-1);
  93.     openfiles();
  94.     while (times--) {
  95.         showfort(pickfort());
  96.         if (times)
  97.             printf("-------\n");
  98.     }
  99.     closefiles();
  100. }
  101.